//
//  ConfigTableViewController.m
//  MyLottery
//
//  Created by wpapple on 16/5/26.
//  Copyright © 2016年 wpapple. All rights reserved.
//

#import "ConfigTableViewController.h"

@interface ConfigTableViewController ()

@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation ConfigTableViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    }
    return self;
}
  
-(NSArray *)dataArray
{
    if (_plistName == nil) {
        return nil;
    }
    
    if (!_dataArray) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:_plistName ofType:@"plist"];
        
        _dataArray = [NSArray arrayWithContentsOfFile:path];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSDictionary *dict = self.dataArray[section];
    
    NSArray *item = dict[@"items"];
    
    return item.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier ];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    NSDictionary *dict = self.dataArray[indexPath.section];
    
    NSArray *item = dict[@"items"];
    
    cell.textLabel.text = item[indexPath.row][@"title"];
    cell.imageView.image = [UIImage imageNamed:item[indexPath.row][@"image"]];
    
    NSString *accossory = item[indexPath.row][@"accossory"];
    NSString * accessory_image = item[indexPath.row][@"accessory_image"];
    
    if (accossory.length > 0) {
        Class classView = NSClassFromString(accossory);
        id classViewObj = [[classView alloc]init];
        
        if ([classViewObj isKindOfClass:[UIImageView class]]) {
            UIImageView *tempImageView = (UIImageView *)classViewObj;
            
            tempImageView.image = [UIImage imageNamed:accessory_image];
            
            [tempImageView sizeToFit];
        }
        
        cell.accessoryView = classViewObj;
    }else
    {
        cell.accessoryView = nil;
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = self.dataArray[indexPath.section];
    NSArray *item = dict[@"items"];
    
    NSString *targetVC = item[indexPath.row][@"targetVC"];
    NSString *targetVCpath = item[indexPath.row][@"targetVCpath"];
    
    if (targetVC.length > 0) {
        Class classTargetVC = NSClassFromString(targetVC);
        
        ConfigTableViewController *targetVCobj = (ConfigTableViewController *)[[classTargetVC alloc]init];
        
        if (targetVCpath != nil) {
             targetVCobj.plistName = targetVCpath;
        }
        
        targetVCobj.title = item[indexPath.row][@"title"];
        
        [self.navigationController pushViewController:targetVCobj animated:YES];
    }
}

@end
